#!/bin/bash

# 设置 Hadoop 的安装路径
HADOOP_INSTALL_DIR="/opt/hadoop"
HADOOP_DIR=$(basename $(ls -d /opt/hadoop/hadoop-*))

# 启动 Hadoop 服务
start_hadoop() {
    echo "启动 Hadoop 服务..."
    # 使用当前用户（fromsko）启动，而不是 root
    sudo -u fromsko $HADOOP_INSTALL_DIR/$HADOOP_DIR/sbin/start-all.sh
    # sudo -u fromsko $HADOOP_INSTALL_DIR/$HADOOP_DIR/sbin/start-dfs.sh
    # sudo -u fromsko $HADOOP_INSTALL_DIR/$HADOOP_DIR/sbin/start-yarn.sh
    echo "Hadoop 服务已启动。"
}

# 停止 Hadoop 服务
stop_hadoop() {
    echo "停止 Hadoop 服务..."
    sudo -u fromsko $HADOOP_INSTALL_DIR/$HADOOP_DIR/sbin/stop-yarn.sh
    sudo -u fromsko $HADOOP_INSTALL_DIR/$HADOOP_DIR/sbin/stop-dfs.sh
    echo "Hadoop 服务已停止。"
}

# 重启 Hadoop 服务
restart_hadoop() {
    echo "重启 Hadoop 服务..."
    stop_hadoop
    start_hadoop
    echo "Hadoop 服务已重启。"
}

# 根据参数执行相应的操作
case "$1" in
    start)
        start_hadoop
        ;;
    stop)
        stop_hadoop
        ;;
    restart)
        restart_hadoop
        ;;
    *)
        echo "用法: $0 {start|stop|restart}"
        exit 1
        ;;
esac